<?php

namespace App\Admin\Controllers;

use App\Models\Comment;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class CommentController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '商品评价';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Comment);
        $grid->filter(function ($filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            $filter->like('content','内容');
            $filter->like('customer.username','用户名');
            $filter->like('order.order_number','所属订单');
        });
        // 默认ID倒序
        $grid->model()->orderByDesc('id');

        $grid->column('customer.username', '用户名');
        $grid->column('order.order_number', '所属订单');
        $grid->column('product.name', '商品名称');
        $grid->column('content', '内容');
        $grid->column('score', '评分');
        $grid->column('level', '好评/差评')->using(['0' => '好评', '1' => '差评']);
        $grid->column('has_images', '是否有图')->using(['0' => '否', '1' => '是']);
        $grid->column('created_at', '评论时间');

        // 工具栏
        $grid->actions(function ($actions) {
            // 去掉编辑
            $actions->disableEdit();
            // 去掉查看
            $actions->disableView();
        });

        $grid->disableCreateButton();
        $grid->disableExport();

        return $grid;
    }

    public function destroy($id)
    {
        return Comment::where('id', $id)->delete();
    }
}
